<%#
 Copyright 2008-2009 Steven Barth <steven@midlink.org>
 Copyright 2008-2018 Jo-Philipp Wich <jo@mein.io>
 Licensed to the public under the Apache License 2.0.
-%>

<script type="text/javascript">//<![CDATA[
	function wifi_delete(ev) {
		if (!confirm(<%=luci.http.write_json(translate('Really delete this wireless network? The deletion cannot be undone! You might lose access to this device if you are connected via this network.'))%>)) {
			ev.preventDefault();
			return false;
		}

		ev.target.previousElementSibling.value = '1';
		return true;
	}

	function wifi_restart(ev) {
		XHR.halt();

		findParent(ev.target, '.table').querySelectorAll('[data-disabled="false"]').forEach(function(s) {
			s.innerHTML = '<em><%:Wireless is restarting...%></em>';
		});

		(new XHR()).post('<%=url('admin/network/wireless_reconnect')%>/' + ev.target.getAttribute('data-radio'),
			{ token: '<%=token%>' }, XHR.run);
	}

	var networks = [ ];

	document.querySelectorAll('[data-network]').forEach(function(n) {
		networks.push(n.getAttribute('data-network'));
	});

	XHR.poll(5, '<%=url('admin/network/wireless_status')%>/' + networks.join(','), null,
		function(x, st)
		{
			if (st)
			{
				var rowstyle = 1;
				var radiostate = { };

				st.forEach(function(s) {
					var r = radiostate[s.device.device] || (radiostate[s.device.device] = {});

					s.is_assoc = (s.bssid && s.bssid != '00:00:00:00:00:00' && s.channel && s.mode != 'Unknown' && !s.disabled);

					r.up        = r.up        || s.is_assoc;
					r.channel   = r.channel   || s.channel;
					r.bitrate   = r.bitrate   || s.bitrate;
					r.frequency = r.frequency || s.frequency;
				});

				for( var i = 0; i < st.length; i++ )
				{
					var iw = st[i],
					    sig = document.getElementById(iw.id + '-iw-signal'),
					    info = document.getElementById(iw.id + '-iw-status'),
					    disabled = (info && info.getAttribute('data-disabled') === 'true');

					var p = iw.quality;
					var q = disabled ? -1 : p;

					var icon;
					if (q < 0)
						icon = "<%=resource%>/icons/signal-none.png";
					else if (q == 0)
						icon = "<%=resource%>/icons/signal-0.png";
					else if (q < 25)
						icon = "<%=resource%>/icons/signal-0-25.png";
					else if (q < 50)
						icon = "<%=resource%>/icons/signal-25-50.png";
					else if (q < 75)
						icon = "<%=resource%>/icons/signal-50-75.png";
					else
						icon = "<%=resource%>/icons/signal-75-100.png";


					if (sig)
						sig.innerHTML = String.format(
							'<span class="ifacebadge" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>"><img src="%s" /> %d%%</span>',
							iw.signal, iw.noise, icon, p
						);

					if (info)
					{
						if (iw.is_assoc)
							info.innerHTML = String.format(
								'<strong><%:SSID%>:</strong> %h | ' +
								'<strong><%:Mode%>:</strong> %s<br />' +
								'<strong><%:BSSID%>:</strong> %s | ' +
								'<strong><%:Encryption%>:</strong> %s',
									iw.ssid, iw.mode, iw.bssid,
									iw.encryption ? iw.encryption : '<%:None%>'
							);
						else
							info.innerHTML = "<strong>SSID: </strong>" + iw.ssid + " | " + String.format(
								'<strong><%:SSID%>:</strong> %h | ' +
								'<strong><%:Mode%>:</strong> %s<br />' +
								'<em>%s</em>',
									iw.ssid || '?', iw.mode,
									disabled ? '<em><%:Wireless is disabled%></em>'
										     : '<em><%:Wireless is not associated%></em>'
							);
					}
				}

				for (var dev in radiostate)
				{
					var img = document.getElementById(dev + '-iw-upstate');
					if (img)
						img.src = '<%=resource%>/icons/wifi' + (radiostate[dev].up ? '' : '_disabled') + '.png';

					var stat = document.getElementById(dev + '-iw-devinfo');
					if (stat)
						stat.innerHTML = String.format(
							'<strong><%:Channel%>:</strong> %s (%s <%:GHz%>) | ' +
							'<strong><%:Bitrate%>:</strong> %s <%:Mbit/s%>',
								radiostate[dev].channel ? radiostate[dev].channel : '?',
								radiostate[dev].frequency ? radiostate[dev].frequency : '?',
								radiostate[dev].bitrate ? radiostate[dev].bitrate : '?'
						);
				}
			}
		}
	);
//]]></script>
